<template>
	<view @click.stop="openDocument" style="display: flex;flex-direction: row;align-items: center;padding: 32rpx;border-radius:20rpx;background-color: #fff;border: 2rpx solid #ECECEC;box-sizing: border-box;font-size: 0;" :style="{width: width}">
		<view>
			<image class="icon" :src="`/static/chat/${handleIcon()}.png`" mode="widthFix" style="width: 60rpx;"></image>
		</view>
		<view style="display: flex;flex-direction:column;margin-left: 30rpx;width:calc(100% - 90rpx)">
			<text style="font-size: 28rpx;margin-bottom: 5rpx;display: -webkit-box;-webkit-line-clamp: 2;line-clamp: 2;-webkit-box-orient: vertical;white-space: pre-line;overflow: hidden;">{{name}}</text>
			<text style="color: darkgray;font-size: 18rpx;">{{handleSize(size)}}</text>
		</view>
	</view>
</template>
<script>
export default {
	props: {
		src: {
			title: String,
			value: ''
		},
		size: {
			title: String,
			value: ''
		},
		name: {
			title: String,
			value: ''
		},
		width: {
			title: String,
			default: '560rpx'
		},
		chat: {
			title: String,
			default: ''
		},
	},
	data() {
		return {
			
		};
	},
	mounted() {
		
	},
	methods: {
		//(H5不支持)打开文件doc, xls, ppt, pdf, docx, xlsx, pptx
		openDocument(){
			// console.log(393939, this.src)
			// #ifdef H5
			this.getDownloadFile(this.src)
			// #endif
			// #ifdef APP-PLUS
			if(!this.chat){
				let path = this.api.xiaodeObsUrl + this.src
				uni.downloadFile({
					url: path,
					success: function (res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							success: function (res) {
								// console.log('打开文档成功');
							},
							fail: function(err){
								console.log(err);
							}
						});
					}
				});
			}else{
				uni.openDocument({
					filePath: this.src,
					success: function (res) {
						// console.log('打开文档成功');
					},
					fail: function(err){
						console.log(err);
					}
				})
			}
			
			// #endif
		},
		playpapers(url){

			let dload = document.createElement("a");

			dload.download = '';// 设置下载的文件名，默认是'下载'

			dload.href = url;

			document.body.appendChild(dload);

			dload.click();

			dload.remove(); // 下载之后把创建的元素删除

			// uni.showToast({

			// 	title: "正在下载",

			// 	icon: "success"

			// });

		},
		getDownloadFile(path){
			let getUrl = this.chat==1?'file/getDownloadUrl':'file/xiaodeGetDownloadUrl'
			this.api.get(getUrl, {
				fileNames: JSON.stringify([path]),
				isThumbnail: false
			}).then(res=>{
				if(res.data.state != 'ok') return
				let downloadUrl = res.data.downloadUrl || {},
				urls = [];
				urls = Object.values(downloadUrl)[0]

				this.playpapers(urls)

			})
		},
		handleIcon(){
			let type = this.name?this.name.split('.').pop():'', icon = '';
			if(type.indexOf('doc')!=-1){
				icon = 'docx'
			}else if(type.indexOf('video')!=-1 || type.indexOf('mp4')!=-1){
				icon = 'video'
			}else if(type.indexOf('xls')!=-1){
				icon = 'xlsx'
			}else if(type.indexOf('pdf')!=-1){
				icon = 'pdf'
			}else if(type.indexOf('ppt')!=-1){
				icon = 'pptx'
			}else if(type.indexOf('txt')!=-1){
				icon = 'txt'
			}else {
				icon = 'image'
			}
			return icon
		},
		handleSize(size){
			let n = Math.floor(size/1024);
			if(n<1024){
				return `${n}KB`
			}
			if(n>=1024){
				n = Math.floor(size/1024/1024)
				return `${n}MB`
			}
		}
	},
	beforeDestroy() {
		
	}
}
</script>
<style>
	
</style>
